using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using CodRevit.ViewModels;

namespace CodRevit.Commands
{
    [Transaction(TransactionMode.Manual)]
    public class CurveArrayCommand : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var arrayHandler = new CurveArrayHandler(commandData.Application.ActiveUIDocument.Document);
            var externalEvent = ExternalEvent.Create(arrayHandler);

            var window = new Views.CurveArrayView();
            var viewModel = new ViewModels.CurveArrayViewModel(
                commandData.Application,
                window,
                externalEvent,
                arrayHandler);

            window.DataContext = viewModel;

            var positionedWindow = App.LeftTopWindow(commandData, window);
            if (positionedWindow == null || positionedWindow is not Views.CurveArrayView)
            {
                return Result.Failed;
            }

            window = (Views.CurveArrayView)positionedWindow;
            window.Show();
            return Result.Succeeded;
        }
    }
}